Fundamentals of Exporting and Importing Data
One of the great things about bcp
is its ease of use. This section runs through a couple simple examples
and provides full explanations. All tables used here can be found in
the AdventureWorks2008 sample database supplied by Microsoft in SQL Server 2008.
Let’s start by exporting product data from AdventureWorks2008 that may be needed by a sales team for reference in Excel format (a .csv file). To do this, you simply export the Product table data into a comma-delimited file. You need to specify the following with bcp in this case:
The full table name (in this case, AdventureWorks2008.Production.Product)
The direction of bcp (OUT in this case because it is exporting data out)
Data filename to hold the exported data (in this case, products.dat)
The server name DBARCH-LT2\SQL08DE01 for this example (in this case, –S DBARCH-LT2\SQL08DE01)
The username SA (in this case, –U sa)
The password (in this case, –P xyz)
A comma as the column delimiter (in this case, –t ",")
That this should be exported in character data format (in this case, –c)
At the command prompt, you execute the following:
C:> BCP AdventureWorks2008.Production.Product OUT products.dat
–S DBARCH-LT2\SQL08DE01 –U sa
–P xyz –t "," –c
Starting copy...
504 rows copied.
Network packet size (bytes): 4096
Clock Time (ms.): total: 10 Average: (50400.00 rows per sec.)
Here’s a sample of the data in the Products.dat file that was just exported:
1,Adjustable Race,AR-5381,0,0,,
2,Bearing Ball,BA-8327,0,0,,
3,BB Ball Bearing,BE-2349,1,0,,
4,Headset Ball Bearings,BE-2908,0,0,,
. . .
Now let’s look at importing data into SQL Server 2008.
Let’s say that each
salesperson is providing a flat file that contains his or her new sales
orders summaries. These files are emailed to a person in the ordering
department and need to be imported into SQL Server every week. The file
that you will import will be a comma-delimited file (.csv)
that the salesperson created using Excel. The new entries will be the
salesperson’s hottest customer’s totals. A sample input data file
(named BCPHotCustomersPlus.csv) is included on the book’s CD for this chapter.
You need to specify the following with bcp in this case:
The full table name (in this case, AdventureWorks2008.Sales.HotCustomersPlus).
The direction of bcp (IN in this case because it is importing data).
The names of the data files that contain the import data (in this case, BCPHotCustomersPlus.csv). The following is a sample of the input data file (BCPHotCustomersPlus.csv):
268,Cycle Parts and Accessories,S,9,Australia,954,Touring-1000 Yellow,7152.210000
268,Cycle Parts and Accessories,S,9,Australia,955,Touring-1000 Yellow,7152.210000
268,Cycle Parts and Accessories,S,9,Australia,956,Touring-1000 Yellow,5721.768000
268,Cycle Parts and Accessories,S,9,Australia,968,Touring-1000 Blue,5721.768000
. . .
The server name (in this case, –S DBARCH-LT2\SQL08DE01)
The username SA (in this case, –U sa).
The password (in this case, –P xyz).
A comma as the column delimiter (in this case, –t ",").
That this should be exported in character data format (in this case, –c).
The –q option (in this case, –q), to be sure quoted identifiers are handled properly.
At the command prompt, you execute the following:
C:> BCP AdventureWorks2008.Sales.HotCustomersPlus IN BCPHotCustomersPlus.csv
–S DBARCH-LT\SQL08DE01 –U sa –P xyz –t "," –c –q
Starting copy...
4 rows copied.
Network packet size (bytes): 4096
Clock Time (ms.): total : 241 Average : (99.59 rows per sec.)
A quick SELECT * from the HotCustomersPlus table shows the success of this operation:
CustomerID CustomerName CustomerType TerritoryID TerritoryName
268 Cycle Parts and Accessories S 9 Australia 954
268 Cycle Parts and Accessories S 9 Australia 955
268 Cycle Parts and Accessories S 9 Australia 956
268 Cycle Parts and Accessories S 9 Australia 968
. . .
The
sales team can now send in their sales orders as they make sales. This
brief example illustrates the beauty and power of using bcp.
The following sections look at how bcp can work with basic data representations (character, native, or Unicode), the use of a format file, and a few other extended bcp capabilities.